@{
    ViewData["Title"] = "库存现有量";
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>库存现有量管理</title>
    <script src="~/libs/scripts/jquery-3.4.1.min.js"></script>
    <link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
    <style>
        /* 页面整体布局 */
        body {
            margin: 0;
            padding: 0;
            background: #f2f3f5;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        /* 主容器 */
        .main-container {
            display: flex;
            height: 100vh;
            gap: 10px;
            padding: 10px;
            box-sizing: border-box;
        }

        /* 左侧物料分类树 */
        .left-panel {
            width: 300px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
        }

        .left-panel-header {
            padding: 15px 20px;
            border-bottom: 1px solid #e8e8e8;
            background: #fafafa;
            border-radius: 8px 8px 0 0;
        }

        .left-panel-header h3 {
            margin: 0;
            font-size: 16px;
            color: #333;
            font-weight: 600;
        }

        .tree-container {
            flex: 1;
            padding: 15px;
            overflow-y: auto;
        }

        /* 右侧内容区域 */
        .right-panel {
            flex: 1;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
        }

        /* 搜索区域 */
        .search-container {
            padding: 20px;
            border-bottom: 1px solid #e8e8e8;
            background: #fafafa;
            border-radius: 8px 8px 0 0;
        }

        .search-form {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            align-items: flex-end;
        }

        .search-item {
            display: flex;
            flex-direction: column;
            min-width: 200px;
        }

        .search-item label {
            margin-bottom: 5px;
            font-size: 14px;
            color: #333;
            font-weight: 500;
        }

        .search-item input,
        .search-item select {
            height: 40px;
            border: 1px solid #d9d9d9;
            border-radius: 6px;
            padding: 0 12px;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .search-item input:focus,
        .search-item select:focus {
            border-color: #40a9ff;
            box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
        }

        /* 按钮样式 */
        .btn-search {
            background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
            border: none;
            border-radius: 6px;
            padding: 10px 24px;
            color: white;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 12px rgba(24, 144, 255, 0.4);
        }

        .btn-search:hover {
            background: linear-gradient(135deg, #096dd9 0%, #0050b3 100%);
            transform: translateY(-1px);
            box-shadow: 0 6px 16px rgba(24, 144, 255, 0.6);
        }

        .btn-reset {
            background: #f5f5f5;
            border: 1px solid #d9d9d9;
            border-radius: 6px;
            padding: 10px 24px;
            color: #666;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .btn-reset:hover {
            background: #e6f7ff;
            border-color: #40a9ff;
            color: #1890ff;
        }

        /* 表格区域 */
        .table-container {
            flex: 1;
            padding: 20px;
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }

        .table-wrapper {
            flex: 1;
            overflow: hidden;
        }

        /* 覆盖layui样式 */
        .layui-table {
            margin: 0;
        }

        .layui-table th {
            background: #f8f9fa !important;
            color: #333 !important;
            font-weight: 600 !important;
            border-bottom: 2px solid #e8e8e8 !important;
        }

        .layui-table tbody tr:hover {
            background-color: #f0f7ff !important;
        }

        .layui-table-cell {
            height: auto !important;
            line-height: 20px !important;
            padding: 12px 10px !important;
        }

        /* 分页样式 */
        .layui-laypage {
            margin-top: 20px;
            text-align: right;
        }

        .layui-laypage > * {
            margin-left: 5px;
        }

        /* 树形组件样式 */
        .layui-tree {
            background: transparent;
        }

        .layui-tree-node {
            padding: 5px 0;
        }

        .layui-tree-txt {
            font-size: 14px;
            padding: 8px 12px;
            border-radius: 4px;
            transition: all 0.3s ease;
        }

        .layui-tree-txt:hover {
            background: #e6f7ff;
            color: #1890ff;
        }

        .layui-tree-node-selected .layui-tree-txt {
            background: #1890ff !important;
            color: white !important;
        }

        /* 状态标签 */
        .status-valid {
            color: #52c41a;
            background: #f6ffed;
            border: 1px solid #b7eb8f;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 12px;
        }

        .status-invalid {
            color: #ff4d4f;
            background: #fff2f0;
            border: 1px solid #ffccc7;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="main-container">
        <!-- 左侧物料分类树 -->
        <div class="left-panel">
            <div class="left-panel-header">
                <h3><i class="layui-icon layui-icon-tree"></i> 物料产品分类</h3>
            </div>
            <div class="tree-container">
                <div id="materialTypeTree"></div>
            </div>
        </div>

        <!-- 右侧内容区域 -->
        <div class="right-panel">
            <!-- 搜索区域 -->
            <div class="search-container">
                <div class="search-form" lay-filter="searchForm">
                    <div class="search-item">
                        <label>产品物料编号</label>
                        <input type="text" id="productMaterielCode" placeholder="请输入产品物料编号">
                    </div>
                    <div class="search-item">
                        <label>产品物料名称</label>
                        <input type="text" id="productMaterielName" placeholder="请输入产品物料名称">
                    </div>
                    <div class="search-item">
                        <label>仓库名称</label>
                        <input type="text" id="warehouseName" placeholder="请输入仓库名称">
                    </div>
                    <div class="search-item">
                        <label>供应商名称</label>
                        <input type="text" id="supplierName" placeholder="请输入供应商名称">
                    </div>
                    <div class="search-item">
                        <label>库存有效性</label>
                        <select id="inventoryValidity">
                            <option value="">全部</option>
                            <option value="true">有效</option>
                            <option value="false">无效</option>
                        </select>
                    </div>
                    <div class="search-item">
                        <button type="button" class="btn-search" onclick="searchData()">
                            <i class="layui-icon layui-icon-search"></i> 搜索
                        </button>
                        <button type="button" class="btn-reset" onclick="resetSearch()">
                            <i class="layui-icon layui-icon-refresh"></i> 重置
                        </button>
                    </div>
                </div>
            </div>

            <!-- 表格区域 -->
            <div class="table-container">
                <div class="table-wrapper">
                    <table id="inventoryTable" lay-filter="inventoryTable"></table>
                </div>
            </div>
        </div>
    </div>

    <!-- 状态模板 -->
    <script type="text/html" id="validityTpl">
        {{# if (d.inventoryValidity) { }}
            <span class="status-valid">有效</span>
        {{# } else { }}
            <span class="status-invalid">无效</span>
        {{# } }}
    </script>

    <script>
        layui.use(['table', 'tree', 'layer', 'util'], function () {
            var table = layui.table;
            var tree = layui.tree;
            var layer = layui.layer;
            var util = layui.util;

            var currentMaterialTypeId = null;
            var tableIns;

            init();

            function init() {
                initMaterialTypeTree();
                initTable();
            }

            function initMaterialTypeTree() {
                $.ajax({
                    url: '/WarehouseManagement/InventoryModel/GetMaterialTypeTree',
                    method: 'GET',
                    success: function (res) {
                        if (res.code === 0 && res.data) {
                            var treeData = [{
                                id: 0,
                                title: '全部分类',
                                children: res.data.map(function (item) {
                                    return convertToTreeNode(item);
                                })
                            }];

                            tree.render({
                                elem: '#materialTypeTree',
                                data: treeData,
                                showCheckbox: false,
                                id: 'materialTypeTreeId',
                                isJump: false,
                                click: function (obj) {
                                    currentMaterialTypeId = obj.data.id === 0 ? null : obj.data.id;
                                    reloadTable();
                                }
                            });
                        } else {
                            layer.msg('获取物料分类失败：' + (res.msg || '未知错误'), { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg('获取物料分类失败', { icon: 2 });
                    }
                });
            }

            function convertToTreeNode(item) {
                var node = {
                    id: item.id || item.value,
                    title: item.label,
                    children: []
                };

                if (item.children && item.children.length > 0) {
                    node.children = item.children.map(function (child) {
                        return convertToTreeNode(child);
                    });
                }

                return node;
            }

            function initTable() {
                tableIns = table.render({
                    elem: '#inventoryTable',
                    url: '/WarehouseManagement/InventoryModel/GetList',
                    page: true,
                    limit: 20,
                    limits: [10, 20, 50, 100],
                    loading: true,
                    height: 'full-220',
                    cellMinWidth: 80,
                    cols: [[
                        { type: 'numbers', title: '序号', width: 70, align: 'center' },
                        { field: 'productMaterielCode', title: '产品物料编号', width: 160, sort: true, align: 'center' },
                        { field: 'productMaterielName', title: '产品物料名称', width: 140, align: 'center' },
                        { field: 'specifications', title: '规格型号', width: 120, align: 'center' },
                        { field: 'quantityInStock', title: '在库数量', width: 100, sort: true, align: 'center' },
                        { field: 'unit', title: '单位', width: 80, align: 'center' },
                        { field: 'inboundBatchNumber', title: '入库批次号', width: 130, align: 'center' },
                        { field: 'warehouseName', title: '仓库', width: 100, align: 'center' },
                        { field: 'warehouseArea', title: '库区', width: 100, align: 'center' },
                        { field: 'warehouseLocation', title: '库位', width: 100, align: 'center' },
                        { field: 'supplierCode', title: '供应商编号', width: 120, align: 'center' },
                        { field: 'supplierName', title: '供应商名称', width: 130, align: 'center' },
                        { field: 'supplierAbbreviation', title: '供应商简称', width: 120, align: 'center' },
                        { field: 'productionWorkOrder', title: '生产工单', width: 120, align: 'center' },
                        { 
                            field: 'inboundDate', 
                            title: '入库日期', 
                            width: 110, 
                            align: 'center',
                            templet: function (d) {
                                return d.inboundDate ? util.toDateString(d.inboundDate, 'yyyy-MM-dd') : '';
                            }
                        },
                        { field: 'inventoryValidity', title: '库存有效性', width: 110, align: 'center', templet: '#validityTpl' }
                    ]],
                    request: {
                        pageName: 'page',
                        limitName: 'limit'
                    },
                    where: getSearchParams(),
                    done: function (res, curr, count) {
                        console.log('表格数据加载完成', res);
                    }
                });
            }

            function getSearchParams() {
                var params = {
                    productMaterielCode: $('#productMaterielCode').val(),
                    productMaterielName: $('#productMaterielName').val(),
                    warehouseName: $('#warehouseName').val(),
                    supplierName: $('#supplierName').val(),
                    inventoryValidity: $('#inventoryValidity').val() || null
                };

                if (currentMaterialTypeId) {
                    params.materialTypeId = currentMaterialTypeId;
                }

                return params;
            }

            window.searchData = function () {
                reloadTable();
            };

            window.resetSearch = function () {
                $('#productMaterielCode').val('');
                $('#productMaterielName').val('');
                $('#warehouseName').val('');
                $('#supplierName').val('');
                $('#inventoryValidity').val('');
                currentMaterialTypeId = null;
                
                tree.setChecked('materialTypeTreeId', 0);
                
                reloadTable();
            };

            function reloadTable() {
                tableIns.reload({
                    where: getSearchParams(),
                    page: {
                        curr: 1
                    }
                });
            }
        });
    </script>
</body>
</html> 